package org.xxpay.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xxpay.core.entity.AlipayCashUser;
import org.xxpay.core.entity.AlipayCashUserExample;
import org.xxpay.core.service.IAlipayCashUserService;
import org.xxpay.service.dao.mapper.AlipayCashUserMapper;

import java.util.List;

@Service
public class AlipayCashUserServiceImpl implements IAlipayCashUserService {

    @Autowired
    private AlipayCashUserMapper recordMapper;

    @Override
    public List<AlipayCashUser> select(int pageIndex, int pageSize, AlipayCashUser record) {
        AlipayCashUserExample exa = getFullExample(record);
        exa.setOrderByClause("CreateTime desc");
        exa.setOffset(pageIndex);
        exa.setLimit(pageSize);
        return recordMapper.selectByExample(exa);
    }

    @Override
    public int count(AlipayCashUser record) {
        return recordMapper.countByExample(getFullExample(record));
    }

    @Override
    public AlipayCashUser findById(int id) {
        return recordMapper.selectByPrimaryKey(id);
    }

    @Override
    public int add(AlipayCashUser record) {
        return recordMapper.insertSelective(record);
    }

    @Override
    public int update(AlipayCashUser record) {
        return recordMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int delete(int id) {
        return recordMapper.deleteByPrimaryKey(id);
    }


    @Override
    public List<AlipayCashUser> selectByAvailableWithMgrUserIds(List<Integer> mgrUserIds){
        return recordMapper.selectByAvailableWithMgrUserIds(mgrUserIds);
    }

    @Override
    public List<AlipayCashUser> listByAvailable() {

        AlipayCashUser record = new AlipayCashUser();
        record.setStatus(AlipayCashUser.STATE_AVAILABLE);
        return recordMapper.selectByExample(getFullExample(record));
    }

    @Override
    public int resetTodayMoney() {
        return recordMapper.resetTodayMoney();
    }

    @Override
    public List<AlipayCashUser> selectCountByMgrUser() {
        return recordMapper.selectCountByMgrUser();
    }

    @Override
    public List<AlipayCashUser> findByAlipayLoginId(String alipayLoginId) {
        AlipayCashUser record = new AlipayCashUser();
        record.setAlipayLoginId(alipayLoginId);
        return recordMapper.selectByExample(getFullExample(record));
    }


    private AlipayCashUserExample getFullExample(AlipayCashUser record){

        if(record == null) return new AlipayCashUserExample();

        AlipayCashUserExample exa = new AlipayCashUserExample();
        AlipayCashUserExample.Criteria c = exa.createCriteria();
        if(record.getId() != null) c.andIdEqualTo(record.getId());
        if(record.getStatus() != null) c.andStatusEqualTo(record.getStatus());
        if(record.getAlipayLoginId() != null) c.andAlipayLoginIdEqualTo(record.getAlipayLoginId());
        if(record.getCreateUserId() != null) c.andCreateUserIdEqualTo(record.getCreateUserId());

        return exa;
    }
}
